import irrlicht;
import std.stdio;
static import core.memory;

void main()
{
    E_DRIVER_TYPE driverType = driverChoiceConsole();
    assert(driverType != E_DRIVER_TYPE.EDT_COUNT);
    
    IrrlichtDevice device = createDevice(driverType, new UintDimension2D(640, 480), 16, false, false, false, null);
    
	assert(device, "Unable to create device.");
	
	//scope(exit)clear(device);  ??
	
	IVideoDriver driver = device.getVideoDriver();
    
    ITexture images = driver.getTexture("media/2ddemo.png");
    driver.makeColorKeyTexture(images, new IntVector2D(0,0));
    
    IGUIFont font = device.getGUIEnvironment().getBuiltInFont();
    IGUIFont font2 = device.getGUIEnvironment().getFont("media/fonthaettenschweiler.bmp");
    
    auto imp1 = new IntRect(349, 15, 385, 78);
    auto imp2 = new IntRect(387, 15, 423, 78);
    
    driver.getMaterial2D().TextureLayer[0].BilinearFilter=true;
    driver.getMaterial2D().AntiAliasing = E_ANTI_ALIASING_MODE.EAAM_FULL_BASIC;
    
    while(device.run() && driver)
    {
        if(device.isWindowActive())
        {
            uint time = device.getTimer().getTime();
            
            driver.beginScene(true, true, new SColor(255, 120, 102, 136));
            driver.draw2DImage(images, new IntVector2D(50, 50),
                               new IntRect(0,0,342,224), null,
                               new SColor(255,255,255,255), true);
            driver.draw2DImage(images, new IntVector2D(164,125),
                               (time/500 % 2) ? imp1 : imp2, null,
                               new SColor(255,255,255,255), true);
            driver.draw2DImage(images, new IntVector2D(270,105),
                               (time/500 % 2) ? imp1 : imp2, null,
                               new SColor(255,time % 255,255,255), true);
            if(font)
                font.draw("This demo shows that Irrlicht is also capable of drawing 2D graphics.",
                          new IntRect(130,10,300,50),
                          new SColor(255,255,255,255));
            
            if(font2)
                font2.draw("Also mixing with 3d graphics is possible.",
                           new IntRect(130,20,300,60),
                           new SColor(255, time % 255, time % 255, 255));
            
            driver.enableMaterial2D();
            driver.draw2DImage(images, new IntRect(10,10,108,48), new IntRect(354,87,442,118));
            driver.enableMaterial2D(false);
            
            IntVector2D m = device.getCursorControl().getPosition();
            driver.draw2DRectangle(new SColor(100,255,255,255),
                                   new IntRect(m.X-20, m.Y-20, m.X+20, m.Y + 20));
            driver.endScene();
        }
    }
}